
package suncertify.middle;

//~--- non-JDK imports --------------------------------------------------------

import suncertify.db.Data;
import suncertify.db.DbExceptions.RecordNotFoundException;

//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import suncertify.shared.DefaultConfig;
import suncertify.shared.ServerProperty;

/**
 *
 * @author jshah
 * This is the stand alone implementation of <code>ClientBroker</code> Interface which is used if the client selects stand alone mode
 */
public class DirectBroker implements ClientBroker {

    /**
     * Instance of the low level Database Access class <code>Data</code>
     */
    Data     rmanager;
    /**
     * location of the database file
     */
    private String dbPath;

    /**
     * default constructor
     */
    public DirectBroker() {
       
    }


   public boolean setDbPath(String path){
       try {
           
            this.dbPath = ServerProperty.getClientDBFileProp();
            rmanager = new Data(dbPath, "rw");
        } catch (IOException e) {

            Logger.getLogger(DirectBroker.class.getName()).log(Level.SEVERE, "ERROR INSTANTIATING BROKER, CANNOT FIND DATABASE FILE", e);
            return false;
        }
        return true;
    }
    public ArrayList<String[]> searchRecords(String name, String Location) {

        String[] criteria = { name, Location };
        int[]    found    = rmanager.find(criteria);

        if (found == null) {
            return null;
        }

        ArrayList<String[]> dataRows = new ArrayList<String[]>();

        for (int i : found) {
            try {
                dataRows.add(rmanager.read(i));
            } catch (RecordNotFoundException ex) {
                Logger.getLogger(DirectBroker.class.getName()).log(Level.SEVERE,"RECORD DOES NOT EXIST", ex);
                return null;
            }
        }

        return dataRows;
    }

    public String book(int recNo, String customerID) {
        byte errorcode = rmanager.book(recNo, customerID);
        switch (errorcode){
            case 0:
                return "BOOKING SUCCESSFUL";
            case Data.SECURITYERROR:
                return "RECORD IS LOCKED TRY LATER";
            case Data.RECNOTFOUND:
                return "RECORD NOT FOUND, IT MIGHT BE DELETED";
            default:
                return "UNKNOWN ERROR, RECORD NOT BOOKED";
        }

    }

    public String unbook(int recNo) {
        byte errorcode = rmanager.unbook(recNo);
          switch(errorcode){
            case 0:
                return "RECORD SUCCESSFULLY UNBOOKED";
            case Data.RECNOTFOUND:
                return "RECORD NOT FOUND, IT MIGHT BE DELETED";
            case Data.INVALIDBOOKING:
                return "RECORD SHOULD BE BOOKED FIRST TO UNBOOK";
            case Data.SECURITYERROR:
                return "RECORD IS LOCKED BY ANOTHER CSR TRY LATER";
            default:
                return "UNKNOWNERROR OCCURED,RECORD NOT UNBOOKED";
        }
    }

    public String[] read(int recNo) {
        try {
            return rmanager.read(recNo);
        } catch (Exception e) {
           Logger.getLogger(DirectBroker.class.getName()).log(Level.SEVERE, "RECORD NOT FOUND IN DB FILE", e);;

            return null;
        }
    }

    public boolean setDbPath(){

        //not implemented for local mode
        return false;
    }
}

